#!/bin/bash
# Fixed Disk filesystem mounting	-*- shell-script -*-

#Function for parsing command line options with "=" in them
# get_opt("init=/sbin/init") will return "/sbin/init"
get_opt() {
    echo "$@" | cut -d "=" -f 2
}

mountroot ()
{
    CFG_FILE=/etc/moblin-initramfs.cfg
    QUIET="$(grep "quiet" /proc/cmdline)"

    if [ -f ${CFG_FILE} ]
    then
        . ${CFG_FILE}
    else
        if [ "$QUIET" == "" ]; then
                echo "Did not find config file: ${CFG_FILE}"
        fi
        sleep 5
        halt
    fi


    CMDLINE=$(cat /proc/cmdline)

    #Process command line options
    XBMC_PARAMS=""
    for i in ${CMDLINE}; do
        case "${i}" in
            xbmc\=*)
                XBMC_PARAMS=$(get_opt $i)
                ;;
        esac
    done

    AUTOGPU="$( echo $XBMC_PARAMS | grep "autogpu" )"

    if [ "$AUTOGPU" != "" ]; then
        # Needs lspci in initramfs, not yet there
        if [ "$QUIET" = "" ]; then
             echo "found auto"
        fi
        XBMC_AMD="$(lspci -nn | grep 0300 | grep 1002)"
        XBMC_NVIDIA="$(lspci -nn | grep 0300 | grep 10de)"
    else
        XBMC_NVIDIA="$( echo $XBMC_PARAMS | grep "nvidia" )"
        XBMC_AMD="$( echo $XBMC_PARAMS | grep "amd" )"
    fi

    BOOTMEDIAMNTPOINT=/bootmediamnt
    mkdir -p $BOOTMEDIAMNTPOINT
    mkdir -p /squashmnt1
    mkdir -p /squashmnt2
    mkdir -p /persistmnt

    if [ "$ROOT" != "" ]; then
        if [ "$QUIET" == "" ]; then
	        echo "will mount root from ${ROOT}"
	fi

	mount -t vfat -o rw,umask=000 $ROOT $BOOTMEDIAMNTPOINT 2> /dev/null
        while [ ! -e $BOOTMEDIAMNTPOINT/rootfs.img ]; do
            /bin/sleep 1
            if [ "$QUIET" == "" ]; then
                    echo "Trying again $ROOT ...."
            fi
	    mount -t vfat -o rw,umask=000 $ROOT $BOOTMEDIAMNTPOINT 2> /dev/null
        done
    else
        # Find the disk drive
        found="no"
	while true
	do
		for device in 'sda' 'sdb' 'sdc' 'sdd' 'sde' 'sdf' 'sdg'; do
			if [ "$QUIET" == "" ]; then
				echo "checking device /dev/${device} for installation source..."
			fi
			if [ -b /dev/${device} ]; then
				if [ -e /sys/block/${device}/removable ]; then
					if [ "$(cat /sys/block/${device}/removable)" = "0" ]; then
						if [ "$QUIET" == "" ]; then
							echo "Found disk drive at /dev/${device}"
						fi
						mount -t vfat -o rw,umask=0 /dev/${device}1 $BOOTMEDIAMNTPOINT 2> /dev/null
						if [ -f $BOOTMEDIAMNTPOINT/rootfs.img ] ; then
							if [ "$QUIET" == "" ]; then
								echo "Found Boot drive at /dev/${device}1"
							fi
							found="yes"
						fi
						umount $BOOTMEDIAMNTPOINT 2> /dev/null
						if [ "$found" = "yes" ]; then
							break;
						fi
						if [ "$QUIET" == "" ]; then
							echo "/dev/${device}1 does not contain a rootfs"
						fi
					fi
				fi
			fi
		done
		if [ "$found" = "yes" ]; then
			break;
		fi
		/bin/sleep 5
	done
	if [ "$QUIET" == "" ]; then
		echo "will mount root from /dev/${device}1"
	fi

	mount -t vfat -o rw,umask=000 /dev/${device}1 $BOOTMEDIAMNTPOINT 2> /dev/null

	while [ ! -e $BOOTMEDIAMNTPOINT/rootfs.img ]; do
		/bin/sleep 1
		if [ "$QUIET" == "" ]; then
			echo "Trying again /dev/${device} ...."
		fi
		mount -t vfat -o rw,umask=000 /dev/${device}1 $BOOTMEDIAMNTPOINT 2> /dev/null
	done
    fi

    XBMC_BOOTTORAM="$( echo $XBMC_PARAMS | grep "boottoram" )"

    if [ "$XBMC_BOOTTORAM" != "" ]; then
        if [ "$QUIET" == "" ]; then
            echo "Copying boot media to RAM ...."
        fi

	# TODO calc size from boot media
	mkdir /bootmediamntRAM
	mount -t tmpfs -o size=500M none /bootmediamntRAM
	cp -R $BOOTMEDIAMNTPOINT/* /bootmediamntRAM 2> /dev/null
	umount $BOOTMEDIAMNTPOINT
	$BOOTMEDIAMNTPOINT=/bootmediamntRAM
    fi

    mount -o ro,loop -t squashfs $BOOTMEDIAMNTPOINT/rootfs.img /squashmnt1

    if [ "$XBMC_NVIDIA" != "" ]; then
        if [ -f $BOOTMEDIAMNTPOINT/restrictedDrivers.nvidia.img ]; then
               if [ "$QUIET" = "" ]; then
                   echo "Mounting NVIDIA drivers..."
               fi
               mount -o ro,loop,noatime,nodiratime $BOOTMEDIAMNTPOINT/restrictedDrivers.nvidia.img /squashmnt2
        fi
    else
        if [ "$XBMC_AMD" != "" ]; then
            if [ -f $BOOTMEDIAMNTPOINT/restrictedDrivers.amd.img ]; then
                if [ "$QUIET" = "" ]; then
		    echo "Mounting AMD drivers..."
                fi
                mount -o ro,loop,noatime,nodiratime $BOOTMEDIAMNTPOINT/restrictedDrivers.amd.img /squashmnt2
            fi
        else
            mount -t tmpfs -o noatime,nodiratime none /squashmnt2

            if [ "$QUIET" = "" ]; then
                echo "Defaulting to Xorg autodetect..."
            fi
        fi
    fi

    if [ -f $BOOTMEDIAMNTPOINT/ext3fs.img ]; then
        mount -o rw,loop,noatime,nodiratime $BOOTMEDIAMNTPOINT/ext3fs.img /persistmnt
    else
        mount -t tmpfs -o noatime,nodiratime none /persistmnt
    fi

    mount -t unionfs -o dirs=/persistmnt=rw:/squashmnt2=ro:/squashmnt1=ro none ${rootmnt}

    # Correct the permissions of /:
    chmod 755 "${rootmnt}"

    # Make sure the individual ro and rw mounts are accessible from within the root
    # once the union is assumed as /.  This makes it possible to access the
    # component filesystems individually.
    if [ ! -e "${rootmnt}/.bootMedia" ]; then
	  mkdir "${rootmnt}/.bootMedia" 
    fi
    #mkdir "${rootmnt}/.ro1" "${rootmnt}/.ro2" "${rootmnt}/.rw"

    mount --bind $BOOTMEDIAMNTPOINT        "${rootmnt}/.bootMedia"
    #mount --bind /squashmnt1 "${rootmnt}/.ro1"
    #mount --bind /squashmnt2 "${rootmnt}/.ro2"
    #mount --bind /persistmnt "${rootmnt}/.rw"

    if [ -f $BOOTMEDIAMNTPOINT/config/fstab ]; then
        cp $BOOTMEDIAMNTPOINT/config/fstab ${rootmnt}/etc/fstab
	rm $BOOTMEDIAMNTPOINT/config/fstab
    fi

    if [ -f $BOOTMEDIAMNTPOINT/ext3fs.img ]; then
        XBMC_TEMPFS="$(echo $XBMC_PARAMS | grep "tempfs" )"
        if [ "$XBMC_TEMPFS" != "" ]; then
	    ALREADY_DONE="$(cat ${rootmnt}/etc/fstab | grep "tmpfs" )"
            if [ "$ALREADY_DONE" = "" ]; then
		  echo "tmpfs /var/log/apt   tmpfs defaults 0 0" >> ${rootmnt}/etc/fstab
		  echo "tmpfs /var/log       tmpfs defaults 0 0" >> ${rootmnt}/etc/fstab
		  echo "tmpfs /tmp           tmpfs defaults 0 0" >> ${rootmnt}/etc/fstab
		  echo "tmpfs /var/tmp       tmpfs defaults 0 0" >> ${rootmnt}/etc/fstab
	    fi
        fi
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/shadow ]; then
        cp $BOOTMEDIAMNTPOINT/config/shadow ${rootmnt}/etc
	rm $BOOTMEDIAMNTPOINT/config/fstab
	chmod 640 ${rootmnt}/etc/shadow
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/rc.local ]; then
        cp $BOOTMEDIAMNTPOINT/config/rc.local ${rootmnt}/etc
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/xorg.conf ]; then
	cp $BOOTMEDIAMNTPOINT/config/xorg.conf ${rootmnt}/etc/X11
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/lircd.conf ]; then
        cp $BOOTMEDIAMNTPOINT/config/lircd.conf ${rootmnt}/etc/lirc
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/hardware.conf ]; then
        cp $BOOTMEDIAMNTPOINT/config/hardware.conf ${rootmnt}/etc/lirc
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/interfaces ]; then
	cp $BOOTMEDIAMNTPOINT/config/interfaces ${rootmnt}/etc/network
    fi
}
